package com.zhou.JavaAndroidFirm.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.app.AppConstant;
import com.zhou.JavaAndroidFirm.base.BaseFragment;
import com.zhou.JavaAndroidFirm.event.PhotoTabEvent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;

/**
 * @author Administrator
 * @date 2018/8/18 0018
 * @des 图文新闻详情
 */
public class PhotosDetailFragment extends BaseFragment {
    @BindView(R.id.photo_view)
    PhotoView photoView;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    private String mImgSrc;

    @Override
    protected void lazyFetchData() {

    }


    @Override
    protected int getLayoutId() {
        return R.layout.fra_news_photo_detail;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initView(Bundle bundle) {
        if (getArguments() != null) {
            mImgSrc = getArguments().getString(AppConstant.PHOTO_DETAIL_IMGSRC);
        }
        Glide.with(mContext)
                .asBitmap()
                .load(mImgSrc)
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }
                })
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .format(DecodeFormat.PREFER_ARGB_8888)
                        .placeholder(R.drawable.ic_image_loading)
                        .error(R.drawable.ic_empty_picture)
                )
                .into(photoView);
    }

    @Override
    protected void initEvent() {
        photoView.setOnPhotoTapListener(new OnPhotoTapListener() {
            @Override
            public void onPhotoTap(ImageView view, float x, float y) {
                EventBus.getDefault().post(new PhotoTabEvent());
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
